package com.evola.rm.model
{
	import com.evola.rm.utils.Flags;

	import mx.collections.ListCollectionView;

	[Bindable]
	public class Language
	{

		private static var FLAG_PREFIX:String="/../assets/images/flags/";

		public var name:String;
		public var code:String;
		public var path:String;
		public var items:ListCollectionView;
		public var itemsHash:Object;

		public function Language()
		{
		}

		public function get flag():*
		{

			var flagCode:String=code.split("_")[1];
			flagCode=flagCode.toLowerCase();

			return Flags.getFlag(flagCode);
		}

		public function getItem(expression:String):ResourceItem
		{

			var item:ResourceItem=itemsHash[expression];

			if (item)
				return item;
			else
				return null;
		}

		public function toPropertiesText():String
		{

			var text:String="";

			for each (var item:ResourceItem in items)
			{
				if (text)
					text+="\n";

				if (item.comment)
					text+=item.comment + "\n";

				text+=item.key.expression + "=" + item.value;
			}

			return text;
		}

		public function addItem(newItem:ResourceItem):Boolean
		{

			//dodajemo novi item i u kolekciju i u  hash
			if (itemsHash.hasOwnProperty(newItem.key.expression))
				return false;

			items.addItem(newItem);
			itemsHash[newItem.key.expression]=newItem;

			return true;
		}
	}
}
